package 年报补充;

import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.bson.Document;

import javax.print.Doc;
import javax.xml.crypto.dsig.spec.XSLTTransformParameterSpec;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FilterDoc {
    static MongoClient mongoClient = new MongoClient("192.168.1.173", 27017);
    static MongoDatabase mDatabase = mongoClient.getDatabase("巨潮网络");
    static MongoCollection mCollection = mDatabase.getCollection("年报补充");
    static MongoCollection Collection = mDatabase.getCollection("年报补充_filter");

    public static void main(String[] args) throws IOException, InvalidFormatException {
        List<Document> docList = getDocFromDb();
        List<String> secCodeList = getSecCodeFromExcel();
//        docList.forEach(doc -> {
//            if (secCodeList.contains(doc.getString("secCode"))){
//                Collection.insertOne(doc);
//                System.out.println(doc.getString("secName") + "写入数据库");
//            }
//        });
        List<String> docSecCodeList = getDocSecCodeList(docList);
        docSecCodeList.forEach(secCode -> {
            if (secCodeList.contains(secCode))
                secCodeList.remove(secCode);
        });

        secCodeList.forEach(secCode -> System.out.println(secCode));
    }

    public static List<String> getSecCodeFromExcel() throws IOException, InvalidFormatException {
        Workbook workbook = WorkbookFactory.create(new File("D:\\测试临时文件\\缺失股票代码.xlsx"));
        Sheet sheet = workbook.getSheetAt(0);

        List<String> secCodeList = new ArrayList<>();
        final int ROW_SIZE = sheet.getPhysicalNumberOfRows();
        for (int i = 1; i < ROW_SIZE; i++) {
            secCodeList.add(sheet.getRow(i).getCell(0)+"");
        }
        return secCodeList;
    }

    public static List<Document> getDocFromDb(){
        FindIterable<Document> findIterable = mCollection.find();

        List<Document> resList = new ArrayList<>();
        for (Document doc : findIterable){
            resList.add(doc);
        }
        return resList;
    }

    public static List<String> getDocSecCodeList(List<Document> docList){
        List<String> resList = new ArrayList<>();
        docList.forEach(doc -> {
            resList.add(doc.getString("secCode"));
        });
        return resList;
    }
}
